using UnityEngine;
using System.Collections;

public class Lance : Trap {

    public GameObject arrowPrefab;
    private GameObject[] poolOfArrow = new GameObject[5];
    private int iNext = 0;
    public float speedShot;

    public override void SetTrapType()
    {
        typeTrap = Const.TRAP_TYPE.LANCE;
    }

    void Start()
    {
        typeTrap = Const.TRAP_TYPE.LANCE;
        for (int i = 0; i < poolOfArrow.Length; i++)
        {
            poolOfArrow[i] = (GameObject)Instantiate(arrowPrefab);
            poolOfArrow[i].SetActive(false);
        }
        this.gameObject.renderer.enabled = false;
        //StartCoroutine(startTrap());
    }

    public override IEnumerator setArcheologistDead()
    {
        yield return new WaitForSeconds(Const._AC_dead_time);
        currentAC.renderer.enabled = false;
    }

    public override void Activate(bool active)
    {
        if (active)
        {
            StartCoroutine(startTrap());
        }
        else
        {
            StopCoroutine("startTrap");
            StopCoroutine("toggleTrap");
            StopAllCoroutines();
        }
        this.renderer.enabled = true;
    }

    public override IEnumerator startTrap()
    {
        yield return new WaitForSeconds(timeStart);
        StartCoroutine(toggleTrap());
    }

    public override IEnumerator toggleTrap()
    {
        activated = true;
        this.renderer.material.mainTexture = texture_piege[0];
        GameObject arrow = poolOfArrow[iNext++];
        if (iNext >= poolOfArrow.Length) iNext = 0;
        arrow.SetActive(true);
        arrow.rigidbody.velocity = Vector3.zero;
        arrow.transform.position = new Vector3(this.transform.position.x, this.transform.position.y + 0.7f, this.transform.position.z);
        arrow.transform.rotation = transform.rotation;
        AudioSource.PlayClipAtPoint(clip[0], this.transform.position);
        arrow.rigidbody.AddForce(transform.forward * speedShot);
        yield return new WaitForSeconds(timeUp);
        activated = false;
        this.renderer.material.mainTexture = texture_piege[1];
        yield return new WaitForSeconds(timeDown);
        StartCoroutine(toggleTrap());

    }
}
